// pages/order/order-partake/order-partake.js
import { debounce, toast } from '../../../utils/Tools'
import { getOrderService,sendOrderService } from '../../../network/order'
Page({
  data: {
    order_id:null,
    show: false,
    name:'',
    phone:'',
    partakeList:[]
  },
  onLoad: function (options) {
    this.setData({
      order_id: options.order_id
    })
    this.handleAddPartake = debounce(this.handleAddPartake,200)

    //    调用网络请求
    this._getOrderService()
  },
  //      函数事件        //
  forbidScroll(e) {
    return;
  },
  handleAddPartake(e){  //  处理添加新参与人
    const {type} = e.currentTarget.dataset
    this.setData({
      [type]: e.detail.value
    })
  },
  handleSheet(e){    // 点击打开上拉菜单
    this.setData({ show: true })
  },
  onClose() {
    this.setData({ show: false,name:'',phone:'' });
  },
  handleSubmit(){   //处理点击添加
    const data = {
      telephone:this.data.phone,
      name: this.data.name,
      order_id: this.data.order_id
    }
    wx.showModal({
      title: '操作提醒',
      content: '是否确认添加参与人',
      success: (result) => {
        if(result.confirm){
          if(!data.telephone&&!data.name){
            wx.showToast({
              title: '请添加内容',
              icon: 'none',
              mask: true
            });
            return;
          }
          sendOrderService(data).then(res=>{
            if(res.code === 200){
              this._getOrderService()
              this.setData({show: false,name:'',phone:''});
              toast('添加成功','success')
            }else{
              toast('添加失败,没有该参与人')
              this.setData({show: false,name:'',phone:''})
            }
          })         
        }else{
          toast('取消操作')
        }
      }
    });
  },
  //      网络请求      //
  async _getOrderService(){  ///   获得参与人
    const res = await getOrderService(this.data.order_id)
    if(res.code === 200){
      this.setData({
        partakeList:res.data
      })
    }
  }
})
